// pages/index/index.js
var api = require('../../utils/api.js');
var app = getApp();
var regeneratorRuntime = require("../../utils/regenerator-runtime/runtime-module.js");
Page({
  // 设置可被分享
  // onShareAppMessage:function(res){
  //   return {title:"分享标题"}
  // },
  // 设置分享到朋友圈的标题
  onShareTimeline: function () {
    return {
      title: "智克艺术 - 首页",
      query: ""
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    activityList: [],
    bannerList: [],
    itemList: ['全部', '舞蹈', '舞蹈', '舞蹈', '舞蹈', '舞蹈', '舞蹈', '舞蹈', '舞蹈', '舞蹈'],
    idx: 0,
    noHide: false,
    videoList: [],
    col1: [],
    col2: []
  },
  //跳转详情
  toDetail(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../activityDetail/activityDetail?id=' + id,
    })
  },
  // 控制导航栏显示隐藏
  hideNavbar() {
    const that = this
    wx.request({
      url: app.globalData.host + 'test/index_a',
      method: 'GET',
      success(res) {
        that.setData({
          noHide: res.data.data == 0 ? false : true
        })
      },
      fail(err) {
        console.log(err)
      }
    })
  },

  async init() {
    await this.getBanner();
    // await this.getActivityList();
    await this.getActiveList();
  },
  toDynamic() {
    wx.navigateTo({
      url: '../activity/activity',
    })
  },
  goVideo() {
    wx.navigateTo({
      url: '../video/video',
    })
  },
  toMusic() {
    wx.switchTab({
      url: '../music/music',
    })
  },
  toStore() {
    wx.switchTab({
      url: '../shopMall/shopMall',
    })
  },
  toTraining() {
    wx.navigateTo({
      url: '/test/pages/training/training',
    })
  },
  toMatch(){
    wx.switchTab({
      url: '/pages/album/album',
    })
  },
  //获取轮播图
  async getBanner() {
    await api.fetchApi('index/getcarousel').then(res => {
      wx.stopPullDownRefresh()
      this.setData({
        bannerList: res.carousel,
        host: res.img_url,
        notice: res.notice
      })
    })

  },
  // 跳转到轮播图详情页
  goStore(e) {
    let url = e.currentTarget.dataset.url
    // console.log(url2)
    wx.reLaunch({
      url: url
    })
  },
  //跳转学校列表
  toSchoolList() {
    wx.navigateTo({
      url: "/pages/schoolList/schoolList"
    })
  },
  // //获取最新动态
  // getActivityList() {
  //    console.log(this.data.host)
  //   api.fetchApi('index/index').then(res => {
  //     var activityList = res
  //     this.setData({
  //       activityList
  //     })
  //   }).catch(err => console.log(err))
  // },

  //获取活动列表
  getActiveList(page) {
    if (this.data.times == 1) {
      return
    }
    api.fetchApi('activity/activity_a', {
      page
    }).then(res => {
      wx.stopPullDownRefresh()
      var activityList = this.data.activityList,
        activityList1 = res;
      if (activityList1.length == 0 && page > 1) {
        // api.showToast('到底啦!', 'http://qnwmfx.laigl.com/iconimg/warn.png', 1500)
        this.setData({
          isMore: true
        })
        return
      }
      activityList = [...activityList, ...activityList1]
      this.setData({
        activityList,
        page,
        times: 1
      })
    })

  },

  //选择课程
  chooseItem(e) {
    var idx = e.currentTarget.id;
    this.setData({
      idx
    })
  },

  //点击更多跳转课程tab
  toMore() {
    wx.switchTab({
      url: '../course/course'
    })
  },

  //点击查看更多跳转活动页面
  pageActivity() {

    wx.navigateTo({
      url: '../activity/activity'
    })

  },

  //获取用户openid
  getOpenId() {
    wx.login({
      success: (res) => {
        //console.log(res)
        wx.request({
          url: app.globalData.host + `Wechat/get_user_info?code=${res.code}`,
          success: (req) => {
            var openid = req.data.data.openid
            app.globalData.openid = openid
            app.globalData.tmplIds = req.data.data.form_id;
            if (!wx.getStorageSync('type')) {
              api.fetchApi('Wechat/add_tourist', {
                openid
              }).then(res => {
                let token = res.token;
                wx.setStorageSync('token', token)
              })
            }
          }

        })
      },
      fail: (err) => {
        // console.log(err)
        wx.showToast({
          title: '登录失败',
          icon: 'none'
        })
      }
    })

  },
  toVideo(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/videoDetail/videoDetail?file_id=' + id
    })
  },
  // 前往视频流
  goscrollVideo() {
    wx.navigateTo({
      url: '/pages/scroll_video/scroll_video',
    })
  },
  getVideoList(page, search) {
    let params = {
      token: wx.getStorageSync('token'),
      page,
      search
    }
    const that = this
    wx.request({
      url: app.globalData.host + 'Video/index',
      method: 'GET',
      // header:{"Accept": "*/*"},
      data: params,
      success(res) {
        let videoList = that.data.videoList
        let videoList1 = res.data.data
        if (page == 1) {
          videoList = res.data.data;
        } else {
          videoList = [...videoList, ...videoList1]
        }
        let col1 = that.data.col1
        let col2 = that.data.col2
        for (var index in videoList) {
          index % 2 == 0 ? col1.push(videoList[index]) : col2.push(videoList[index])
        }
        that.setData({
          col1,
          col2,
          page,
          pageNum: videoList1.length,
        })
      },
      fail(err) {
        wx.showToast({
          title: res.data.msg,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log('二维码参数是',options.scene)
    this.init();
    this.getVideoList()
    this.hideNavbar()
    this.getOpenId()
    // this.getTabbar()
    //从点名页面退出下次进来继续进点名页面
    if (wx.getStorageSync('isDian') == 1 && wx.getStorageSync('type') == 8 && wx.getStorageSync('token') && !app.globalData.err) {
      var params = wx.getStorageSync('params')
      wx.reLaunch({
        url: '../teacherName1/teacherName1?studentParams=' + params.studentParams + '&school=' + params.school + '&project=' + params.project + '&class1=' + params.class1,
      })


    }
    // 清除点名时退出状态
    wx.setStorageSync('isDian', 0);


  },

  //跳转课程详情
  toDetail1() {
    wx.navigateTo({
      url: '../courseDetail/courseDetail',
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // wx.request({
    //   url: 'https://www.fastmock.site/mock/467f81a5f7b55e3a5af9c90e4c1efaff/getBanner/index',
    //   method:'post',
    //   success:res=>{
    //     console.log(res.data.data)
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

    this.init();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hide == false) {
      return
    }
    var page = this.data.page || 1;
    page++;
    wx.showLoading({
      title: '正在加载...',
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 500)
    if (this.data.pageNum < 4) {
      wx.showToast({
        title: '没有更多视频了',
        icon: 'none',
      })
      return
    }
    this.getVideoList(page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})